package Service.Teacher.Imp;

import Dao.DaoFactory;
import Dao.Process.MediumDao;
import Dao.Process.ReportDao;
import Dao.Process.StageCheckDao;
import Dao.Process.TaskDao;
import Dao.TitleAndPaper.PaperDao;
import Entity.Process.StageCheck;
import Service.Teacher.CheckDataService;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;

public class CheckDataServiceImp implements CheckDataService {
    private final Connection conn;
    private final TaskDao taskDao;
    private final PaperDao paperDao;
    private final ReportDao reportDao;
    private final MediumDao mediumDao;
    private final StageCheckDao stageCheckDao;

    public CheckDataServiceImp() {
        conn = DBUtil.getConnByDruid();
        taskDao = DaoFactory.getInstance("taskDao");
        paperDao = DaoFactory.getInstance("paperDao");
        reportDao = DaoFactory.getInstance("reportDao");
        mediumDao = DaoFactory.getInstance("mediumDao");
        stageCheckDao = DaoFactory.getInstance("stageCheckDao");

    }

    /**
     * 审核任务书
     *
     * @param result  审核结果
     * @param opinion 审核意见
     * @param key     任务书的ID
     * @param bh      论文编号
     * @return true 成功 ，false 失败
     */
    @Override
    public Boolean checkAss(String result, String opinion, String key, String bh) {
        boolean flag = false;
        try {
            conn.setAutoCommit(false); //设置不自动提交事务
            //审核任务书 如果通过则 将论文的状态修改为 开题报告  //不通过则不需要
            int res1 = taskDao.updateCheck(Integer.parseInt(key), result, opinion, "审核完成", conn);  //更新任务书的审核状态
            if ("通过".equals(result)) {
                int res2 = paperDao.updateCheck(bh, "开题报告", conn);  //修改论文的状态
                if (res1 == 1 && res2 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            } else {
                if (res1 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }

    /**
     * 审核开题报告
     *
     * @param result  审核结果
     * @param opinion 审核意见
     * @param key     开题报告的ID
     * @param bh      论文编号
     * @return true成功 false 失败
     */
    @Override
    public Boolean checkReport(String result, String opinion, String key, String bh) {
        boolean flag = false;
        try {
            conn.setAutoCommit(false); //设置不自动提交事务
            //审核任务书 如果通过则 将论文的状态修改为 中期检查  //不通过则不需要
            int res1 = reportDao.updateCheck(Integer.parseInt(key), result, opinion, conn);
            if ("通过".equals(result)) {
                //更新开题报告的的审核状态
                //修改论文的状态
                int res2 = paperDao.updateCheck(bh, "中期检查", conn);
                if (res1 == 1 && res2 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            } else {
                if (res1 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }

    /**
     * 审核中期检查
     *
     * @param result  审核结果
     * @param opinion 审核意见
     * @param key     学生的学号
     * @param bh      论文编号
     * @return true 成功 false 失败
     */
    @Override
    public Boolean updateMedium(String result, String opinion, String key, String bh, String TNO) {
        boolean flag = false;
        try {
            conn.setAutoCommit(false); //设置不自动提交事务
            //审核任务书 如果通过则 将论文的状态修改为 中期检查  //不通过则不需要
            int res1 = mediumDao.updateCheckBySNO(key, result, opinion, "审核完成", conn);
            if ("通过".equals(result)) {
                //更新中期检查的的审核状态
                //修改论文的状态
                int res2 = paperDao.updateCheck(bh, "初稿", conn);
                if (res1 == 1 && res2 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            } else {
                if (res1 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }

    /**
     * 审核初稿
     *
     * @param result  审核结果
     * @param opinion 审核意见
     * @param key     审核的数据的数据库依据
     * @param bh      论文编号
     * @return true成功 false 失败
     */
    @Override
    public Boolean checkFirst(String result, String opinion, String key, String bh, String thisState, String nextState) {
        boolean flag = false;
        try {
            conn.setAutoCommit(false); //设置不自动提交事务
            //审核任务书 如果通过则 将论文的状态修改为 中期检查  //不通过则不需要
            int res1 = stageCheckDao.updateCheckBySNOAndState(key, result, opinion, thisState, conn);
            if ("通过".equals(result)) {
                //修改论文的状态
                int res2 = paperDao.updateCheck(bh, nextState, conn);
                if (res1 == 1 && res2 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            } else {
                if (res1 == 1) {
                    conn.commit();
                    flag = true;
                } else {
                    conn.rollback();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }


}
